package com.lktx.bbfactory.controller;

import com.lktx.bbfactory.common.CommonPage;
import com.lktx.bbfactory.common.CommonResult;
import com.lktx.bbfactory.entiy.Project;
import com.lktx.bbfactory.entiy.VO.projectListVo;
import com.lktx.bbfactory.service.projectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/project")
public class ProjectController {

    @Resource
    private projectService projectService;

    //分页查询项目
    @GetMapping("/list")
    public CommonResult<CommonPage<projectListVo>> list(@RequestParam(required = false) String code,
                                                        @RequestParam(required = false) String name,
                                                        @RequestParam(required = false) String client,
                                                        @RequestParam(required = false) String status,
                                                        @RequestParam(required = false) String clientUsername,
                                                        @RequestParam(defaultValue = "1") Integer pageNum,
                                                        @RequestParam(defaultValue = "10") Integer pageSize) {
        CommonPage<projectListVo> page = projectService.listPage(code, name, client, status, clientUsername,pageNum, pageSize);
        return CommonResult.success(page);
    }

    //添加项目
    @PostMapping("/add")
    public CommonResult<String> addProject(@RequestBody Project project) {
        projectService.addProject(project);
        return CommonResult.success("添加成功");
    }

    //更新项目
    @PostMapping("/update")
    public CommonResult<String> updateProject(@RequestBody Project project) {
        projectService.updateProject(project);
        return CommonResult.success("更新成功");
    }

    //删除项目
    @PostMapping("/delete")
    public CommonResult<String> deleteProject(@RequestBody Map<String, Long> param) {
        Long id = param.get("id");
        try {
            projectService.deleteProject(id);
            return CommonResult.success("删除成功");
        } catch (RuntimeException e) {
            return CommonResult.failed(e.getMessage());
        }

    }

    //计算项目进度
    @GetMapping("/progress")
    public CommonResult<Long> progress(@RequestParam Long id) {
        return CommonResult.success(projectService.proProgress(id));
    }
}


